/*
 * Created by JFormDesigner on Sun Jun 06 15:32:24 CST 2021
 */

package com.J1ay.carsys.gui.Emp;

import com.J1ay.carsys.gui.Emp.Dialog.AddCar;
import com.J1ay.carsys.gui.Emp.Dialog.Del;
import com.J1ay.carsys.gui.Emp.Dialog.UpdateCar;
import com.J1ay.carsys.gui.Welcome;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.Car;
import com.J1ay.carsys.service.EmpService;
import com.J1ay.carsys.service.Impl.EmpServiceImpl;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class CarManage extends JFrame {
    int EmpId,len=0;
    Car[] carInfos;
    String Ename;
    EmpService empService = new EmpServiceImpl();
    public CarManage(int empId, String name) {
        initComponents();
        this.empId.setText(empId+"");
        this.name.setText(name);
        this.EmpId = empId;
        this.Ename = name;
    }
    public void getCarList(){
        carInfos= (Car[]) empService.getCarList().getData();
        System.out.println(carInfos);
        String[] data = new String[carInfos.length];
        for (int i = 0 ;i < carInfos.length;++i){
            data[i] = carInfos[i].toString();
        }
        this.carList.setListData(data);
        len = carInfos.length;
    }
    public void start() {
        this.setVisible(true);
        this.getCarList();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    private void exitActionPerformed(ActionEvent e) {
        // TODO add your code here
        new Welcome().start();
        // 关闭窗口
        JButton jButton = (JButton) e.getSource();
        Container container = jButton.getParent();
        while (!(container instanceof JFrame)) {
            container = container.getParent();
        }
        ((JFrame)container).dispose();
    }

    public void refresh(){
       this.getCarList();
        scrollPane1.setViewportView(carList);
        System.out.println(this.carInfos);
    }


    private void mainActionPerformed(ActionEvent e) {
        // TODO add your code here
        new Main(EmpId,Ename).start();
        // 关闭窗口
        JButton jButton = (JButton) e.getSource();
        Container container = jButton.getParent();
        while (!(container instanceof JFrame)) {
            container = container.getParent();
        }
        ((JFrame)container).dispose();
    }



//    新增
    private void button3ActionPerformed(ActionEvent e) {
        // TODO add your code here
        new AddCar(this,len).start();
        this.refresh();
    }

    private void button4ActionPerformed(ActionEvent e) {
        // TODO add your code here
        int index = this.carList.getSelectedIndex();
        if (index == -1) {
            new Warning(this,"您还未选中想要删除的车辆！").start();
        }
        else {
            Car nowcar = carInfos[index];
            new Del(this,nowcar).start();
            System.out.println(nowcar);
            this.refresh();
        }
    }

    private void button5ActionPerformed(ActionEvent e) {
        // TODO add your code here
        int index = this.carList.getSelectedIndex();
        if (index == -1) {
            new Warning(this,"您还未选中想要修改的车辆！").start();
        }
        else {
            Car nowcar = carInfos[index];
            new UpdateCar(this,nowcar).start();
            System.out.println(nowcar);
            this.refresh();
        }
    }




    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        main = new JButton();
        exit = new JButton();
        label2 = new JLabel();
        empId = new JLabel();
        name = new JLabel();
        label5 = new JLabel();
        panel1 = new JPanel();
        panel2 = new JPanel();
        label3 = new JLabel();
        label4 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();
        label10 = new JLabel();
        scrollPane1 = new JScrollPane();
        carList = new JList();
        button3 = new JButton();
        button4 = new JButton();
        button5 = new JButton();

        //======== this ========
        setTitle("\u5c0f\u6c7d\u8f66\u51fa\u79df\u7cfb\u7edf");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u76ee\u524d\u6240\u5728:\u6c7d\u8f66\u7ba1\u7406");

        //---- main ----
        main.setText("\u9996\u9875");
        main.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mainActionPerformed(e);
            }
        });

        //---- exit ----
        exit.setText("\u9000\u51fa");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                exitActionPerformed(e);
            }
        });

        //---- label2 ----
        label2.setText("\u5de5\u53f7:");

        //---- label5 ----
        label5.setText("\u6b22\u8fce\u60a8~");

        //======== panel1 ========
        {
            panel1.setBackground(Color.lightGray);

            //======== panel2 ========
            {

                //---- label3 ----
                label3.setText("\u7f16\u53f7");

                //---- label4 ----
                label4.setText("\u540d\u79f0");

                //---- label6 ----
                label6.setText("\u8f66\u724c");

                //---- label7 ----
                label7.setText("\u7c7b\u578b");

                //---- label8 ----
                label8.setText("\u8f66\u8f86");

                //---- label9 ----
                label9.setText("\u4ef7\u683c/\u5929");

                //---- label10 ----
                label10.setText("\u72b6\u6001");

                GroupLayout panel2Layout = new GroupLayout(panel2);
                panel2.setLayout(panel2Layout);
                panel2Layout.setHorizontalGroup(
                    panel2Layout.createParallelGroup()
                        .addGroup(panel2Layout.createSequentialGroup()
                            .addGap(22, 22, 22)
                            .addComponent(label3, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label4, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label6, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label7, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label8, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label9, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(label10, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                );
                panel2Layout.setVerticalGroup(
                    panel2Layout.createParallelGroup()
                        .addGroup(panel2Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(panel2Layout.createParallelGroup()
                                .addComponent(label3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(label7, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(label6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(label4, GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                                .addComponent(label9, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(label10, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(label8, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                );
            }

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(carList);
            }

            //---- button3 ----
            button3.setText("\u65b0\u589e");
            button3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    button3ActionPerformed(e);
                }
            });

            //---- button4 ----
            button4.setText("\u5220\u9664");
            button4.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    button4ActionPerformed(e);
                }
            });

            //---- button5 ----
            button5.setText("\u4fee\u6539");
            button5.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    button5ActionPerformed(e);
                }
            });

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(panel2, GroupLayout.PREFERRED_SIZE, 586, GroupLayout.PREFERRED_SIZE)
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 580, GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(button4)
                            .addComponent(button3)
                            .addComponent(button5))
                        .addGap(5, 5, 5))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(button3, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(button4)
                        .addGap(68, 68, 68)
                        .addComponent(button5)
                        .addContainerGap(58, Short.MAX_VALUE))
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addComponent(panel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                        .addContainerGap())
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(main, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(label2, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(empId, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(name, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(label5, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(exit, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(exit, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label5)
                        .addComponent(empId)
                        .addComponent(name)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                        .addComponent(main, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JButton main;
    private JButton exit;
    private JLabel label2;
    private JLabel empId;
    private JLabel name;
    private JLabel label5;
    private JPanel panel1;
    private JPanel panel2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;
    private JLabel label10;
    private JScrollPane scrollPane1;
    private JList carList;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
